capture log close
clear all
set maxvar 32767
set more off
pause off
# delimit;

local crime "";
local output "";


/* 
Summary stats for "persist" dataset
*/

local exp "prworapersist";
local main_vars "order_* tm*" ;

local demo_list "a m f" ; 

local bwidth = 264 ;
local bwidth_close = 90 ;

use "`crime'\prep2\crime_prwora_prepforreg_persist.dta", clear ;

keep if abs(dobdd_run) <= `bwidth' ;

/* Subsamples */
gen 	 demo_a = 1 ;
gen byte demo_o	= (n_diag1_mental == 1 & (substr(dibdig,1,3) != "317" & substr(dibdig,1,3) != "318" & substr(dibdig,1,3) != "319" )) ;
gen byte demo_n = (demo_o == 0) ;
gen byte demo_i = (n_diag1_mental == 1 & (substr(dibdig,1,3) == "317" | substr(dibdig,1,3) == "318" | substr(dibdig,1,3) == "319" )) ;
gen byte demo_p = (n_diag1_mental != 1) ;
gen byte demo_y = (n_diag1_mental == 1) ;
gen byte demo_t = (n_fam_singmom != 1 & n_fam_nopar != 1) ;
gen byte demo_s = (n_fam_singmom == 1) | (n_fam_nopar ==1) ;
gen byte demo_m = (n_male ==1) ;
gen byte demo_f = (n_male !=1) ;
gen byte demo_w	= (race == "W") ;
gen byte demo_b = (race == "B") ;
gen byte demo_z = (race != "W") ;

egen cnty_ue97 = rowmean(cnty_urate_1997-cnty_urate_1998) ;
egen cnty_uegr = rowmean(cnty_urate_2007-cnty_urate_2012) ;
egen cnty_ueall = rowmean(cnty_urate_1997-cnty_urate_2017) ;
rename cnty_crimerate cnty_crime ;
rename cnty_share_blue cnty_dem ;
rename cnty_pop2000 cnty_pop ;

foreach cnty in crime dem pop ue97 uegr ueall { ;
	summ cnty_`cnty', det ;
	gen byte demo_`cnty'high = (cnty_`cnty' > r(p50) & cnty_`cnty' != .) ;
	gen byte demo_`cnty'low = (cnty_`cnty' <= r(p50) & cnty_`cnty' != .) ;
	replace demo_`cnty'high = . if cnty_`cnty' == . ;
	replace demo_`cnty'low = . if cnty_`cnty' == . ;
	tab demo_`cnty'high, m ;
	tab demo_`cnty'low, m ;
} ;

summ n_earnhhpre9095, det ;
replace n_earnhhpre9095 = 0 if n_earnhhpre9095 == . ;
gen byte demo_h = (n_earnhhpre9095 > r(p50) & n_earnhhpre9095 != .) ;
gen byte demo_l = (n_earnhhpre9095 <= r(p50) & n_earnhhpre9095 != .) ;
replace demo_h = . if n_earnhhpre9095 == . ;
replace demo_l = . if n_earnhhpre9095 == . ;
tab demo_h, m ;
tab demo_l, m ;

local summstats_lab "" ;
local tot = 0 ;

foreach demo in `demo_list' { ;
	foreach var of varlist 
		`main_vars'
		{ ;
		local summstats_lab "`summstats_lab' `demo'`var' " ;
		local tot = `tot' + 1 ;
	} ;
} ;	/* end demo */

matrix summstats=J(16,`tot',.) ;
local count = 0 ;

foreach demo in `demo_list' { ;
	foreach var of varlist 
		`main_vars'
		{ ;
			
			/* Full sample */
			local count = `count' + 1 ;
			qui summ `var' if abs(dobdd_run) <= `bwidth' & demo_`demo'== 1, det ;
			matrix summstats[1,`count']=r(mean) ;
			matrix summstats[2,`count']=r(sd) ;
			matrix summstats[3,`count']=r(p50) ; 
			matrix summstats[4,`count']=r(N) ;
			
			/* Post Aug 22, 1996 (RHS, review) */
			qui summ `var' if n_post == 1 & abs(dobdd_run) <= `bwidth' & demo_`demo'== 1, det ;
			matrix summstats[5,`count']=r(mean) ;
			matrix summstats[6,`count']=r(sd) ;
			matrix summstats[7,`count']=r(p50) ; 
			matrix summstats[8,`count']=r(N) ;
			
			/* Before (LHS, no review) */
			qui summ `var' if n_post == 0 & abs(dobdd_run) <= `bwidth' & demo_`demo'== 1, det ;
			matrix summstats[9,`count']=r(mean) ;
			matrix summstats[10,`count']=r(sd) ;
			matrix summstats[11,`count']=r(p50) ; 
			matrix summstats[12,`count']=r(N) ;
			
			/* Before (LHS, no review) close to cutoff -- CONTROL MEANS FOR RD */
			qui summ `var' if n_post == 0 & dobmo_run >= -6 & dobmo_run < 0 & demo_`demo'== 1, det ;
			matrix summstats[13,`count']=r(mean) ;
			matrix summstats[14,`count']=r(sd) ;
			matrix summstats[15,`count']=r(p50) ; 
			matrix summstats[16,`count']=r(N) ;
			
	} ;

} ; /* end demo */

clear ;
svmat summstats ; 
rename (summstats*) (`summstats_lab') ;
local samp1 "all" ;
local samp2 "postt" ;
local samp3 "prec" ;
local samp4 "prec_rd" ;
local stat1 "mean" ;
local stat2 "sd" ;
local stat3 "median" ;
local stat4 "N" ;
gen label = "" ;
forval i = 1(1)4 { ;
	forval j = 1(1)4 { ;
		replace label = "`samp`i''_`stat`j''" if _n == (`i' - 1)*4 + `j' ;
	} ;
} ;
order label ;
save "`output'\prworapersist_crime_summstats.dta", replace ;

capture log close;
